<template>
	<div class="symptom" @dblclick="getFirstVisit">
		<el-row>
			<el-col :span="8" :offset="4" style="text-align:center;">症状体征</el-col>
			<el-col :span="4" style="font-size:16pt;text-align:right;cursor:pointer;">	
				<el-popover
				placement="right"
				width="300"
				trigger="click">
				<el-row>
					<el-col :span="24" style="text-align:center;font-size:12pt;font-weight:bolder;">编辑新症状</el-col>
				</el-row>
				<el-row v-for="(item,key) in editSymptomList" :key="key">
					<el-col :span="20">【{{key+1}}】{{item.symptomName}}</el-col>
					<el-col :span="4" style="text-align:right;"><el-button type="text" @click="removeSymptom(key)">删除</el-button></el-col>
				</el-row>
				<el-row>
					<el-col :span="20"><el-input v-model='newSymptom' placeholder="请输入要增加的症状内容"></el-input></el-col>
					<el-col :span="4" style="text-align:right;"><el-button type="text" @click="addSymptom">增加</el-button></el-col>
				</el-row>
				<!-- <i class="el-icon-edit-outline" title="添加新症状" slot="reference"></i> -->
				<el-button size="mini" round slot="reference">添加</el-button>
			</el-popover></el-col>
			<el-col :span="4" style="font-size:16pt;text-align:right;cursor:pointer;">
				<!-- <i class="el-icon-d-arrow-left" title="展开症状列表" @click="openSymptom"></i>
				<i class="el-icon-d-arrow-right" title="获取初诊内容" @click="getFirstVisit"></i> -->
				<el-button size="mini" round @click="openSymptom">展开</el-button>
			</el-col>
			<el-col :span="4" style="font-size:16pt;text-align:center;">
				<el-button size="mini" round @click="getFirstVisit">确定</el-button>
			</el-col>
			<el-col style="margin-top:5px;">
				<el-input placeholder="" v-model="complaint.complaintName">
					<template slot="prepend">主诉</template>
				</el-input>
			</el-col>
		</el-row>
		<div class="symptomContent">
			<el-row v-for="(item,index) in vitalSignList" :key="index">
			<el-col>
				<el-checkbox :label="item.symptomName" disabled checked style="color:#409EFF;">{{item.symptomName}}</el-checkbox>
			</el-col>
		</el-row>
		<el-checkbox-group v-model="checkList">
			<el-row v-show="item.isShow" v-for="(item,index) in symptomList" :key="index">
				<el-col :span="24">
					<el-checkbox :label="item.symptomCode"><span :title="item.symptomName" :class="symptomClass[item.isMutex]">{{item.symptomName}}</span></el-checkbox>
				</el-col>
			</el-row>
		</el-checkbox-group>
		<el-row v-for="(item,index) in editSymptomList" :key="'edit'+index">
			<el-col>
				<el-checkbox :label="item.symptomName" disabled checked>{{item.symptomName}}</el-checkbox>
			</el-col>
		</el-row>
		</div>
	</div>
</template>
<script type="text/javascript">
import bus from '@/eventBus.js'
export default{
	name:'SymptomPanel',
	data(){
		return {
			complaint:{},
			newSymptom:'',
			complaintCode:'',
			symptomList:[],
			checkList:[],
			editSymptomList: [],
			symptomClass:{
				'0':"",
				'1':"mutexStyle1",
				'2':"mutexStyle2"
			}
		}
	},
	methods:{
		getFirstVisit: function(){
			var firstGroups = this.firstGroups.vitalGroups+this.firstGroups.baseInfoGroups;
			var haveList = this.checkList;
			var complaintCode = this.complaintCode;
			var haveContent = '-'+haveList.join('-')+'-';
			var checkSymptomList = [];
			this.symptomList.forEach(function(symptom){
				if (haveContent.indexOf('-'+symptom.symptomCode+'-')<0) {
					symptom.isShow = false;
				}else{
					checkSymptomList.push(symptom);
				}
			});
				//通知初诊组件请求初诊数据
				var findGroupItem = {
					"groupContent": firstGroups + haveList.join(','),
					"complaintCode": complaintCode
				}
				bus.$emit('findFirstVisit',findGroupItem);
				//将组件内信息传入store中共享
				var symptomPanelData = {
					complaint: this.complaint,
					editSymptomList: this.editSymptomList,
					checkSymptomList: checkSymptomList
				};
				this.$store.commit({
					type: "setSymptomPanelData",
					symptomPanelData: symptomPanelData
				});
			
		},
		openSymptom:function(){
			this.changeFlag = true;
				this.symptomList.forEach(function(symptom){
					symptom.isShow = true;
				});
				// this.$store.commit({
				// 		type: "setSymptomPanelData",
				// 		symptomPanelData: {
				// 			complaint:{
				// 				complaintName:''
				// 			}}
				// 	});
		},
		addSymptom:function(){
			if (this.newSymptom != '') {
				this.editSymptomList.push({symptomName:this.newSymptom});
			}
			this.newSymptom = '';
		},
		removeSymptom:function(index){
			this.editSymptomList.splice(index,1);
		},
		setMutex:function(symptomCode,isMutex){
			this.symptomList.forEach(function(symptom){
					if (symptom.symptomCode == symptomCode) {
						symptom.isMutex = isMutex;
					}
				});
		}
	},
	computed:{
		vitalSignList(){
			return this.$store.state.vitalSignList
		},
		firstGroups(){
			return this.$store.state.firstGroups
		},
		recordData(){
			return this.$store.state.recordData;
		},
		reSymptomDone(){
			return this.$store.state.reSymptomDone;
		},
		firstVisit(){
			return this.$store.state.firstVisit;
		}
	},
	created:function(){
			//this在后面指向发生了变化，所以先保存下来
			var vm =this;
			bus.$on('querySymptoms',(menu)=>{
				vm.complaint = menu;
				//在重新绑定数据时，消除症状和可能疾病
				if (menu=="none") {
					vm.symptomList = [];
					vm.checkList = [];
					return;
				}
				var complaintCode = menu.complaintCode;
				vm.$store.commit({
						type: "setComplaintCode",
						complaintCode: complaintCode
					});
				$.ajax({
					type:"get",
					url:CONST.url+"getComplaintByCode",
					data:{"complaintCode": complaintCode},
					success:function(result){
						//告知加载完毕
						vm.$store.commit({
							type: "setReSymptomDone",
							reSymptomDone: "REDONE"
						});
						if (result.code==0||result.code=='0') {
							var dataList = result.data.symptomList;
							dataList.forEach(function(el){
								el.isShow = true;
							});
							vm.symptomList = dataList;
							vm.checkList = [];
							vm.changeFlag = true;
							vm.complaintCode = complaintCode;
							
						}else{
							alert("主诉数据请求失败！");
						}
					},
					error:function(){
						alert("主诉数据请求失败！");
					}
				})
			});
		},
		watch:{
			checkList(curVal,oldVal){
				var haveList = this.checkList;
				var haveContent = '-'+haveList.join('-')+'-';
				var checkSymptomList = [];
				this.symptomList.forEach(function(symptom){
					symptom.isMutex = 0;
					if (haveContent.indexOf('-'+symptom.symptomCode+'-')>=0) {
						checkSymptomList.push(symptom);
					}
				});
				//判断勾选的症状是否存在互斥
				var mutexCode1 = "";
				var mutexCode2 = "";
				for (var i = 0; i < checkSymptomList.length; i++) {
					var symptom = checkSymptomList[i];
					var mutexs = symptom.symptomMutex;
					if (!mutexs) {
						continue;
					}else{
						var mutexArray = mutexs.split(",");
						for (var j =0; j< mutexArray.length; j++) {
							if (haveContent.indexOf('-'+mutexArray[j]+'-')>=0) {
								mutexCode1 = symptom.symptomCode;
								mutexCode2 = mutexArray[j];
								break;
							}
						}
					}
					if (mutexCode1&&mutexCode2) {
						this.setMutex(mutexCode1,1);
						this.setMutex(mutexCode2,1);
						break;
					}
				}
				//将组件内信息传入store中共享
				var symptomPanelData = {
					complaint: this.complaint,
					editSymptomList: this.editSymptomList,
					checkSymptomList: checkSymptomList
				};
				this.$store.commit({
					type: "setSymptomPanelData",
					symptomPanelData: symptomPanelData
				});
			},
			reSymptomDone(curVal,oldVal){
				if (curVal=="REDONE"&&oldVal=="RESEND") {
					//病史体征数据绑定勾选
					var symptom = this.recordData.recordSymptom;
					//允许主诉修改后，加入绑定
					if (this.recordData.recordMainTell) {
						this.complaint.complaintName = this.recordData.recordMainTell;
					}
					var vm =this;
					if (symptom&&symptom!="@@@") {
						var checkSymptom = "###"+symptom.substring(0,symptom.indexOf("@@@"));
						var addSymptom = symptom.substring(symptom.indexOf("@@@")+3,symptom.length);
						vm.editSymptomList=[];
						if (addSymptom) {
							addSymptom.split("###").forEach(function(sp){
								if (sp) {
									vm.editSymptomList.push({symptomName:sp});
								}
							});	
						}
						vm.checkList=[];
						if (checkSymptom) {
							vm.symptomList.forEach(function(sp){
								if (checkSymptom.indexOf("###"+sp.symptomName+'###')>=0) {
									vm.checkList.push(sp.symptomCode);
								}
							});
						}
					}
					//触发初诊内容
					this.$store.commit({
						type: "setReFirstDone",
						reFirstDone: "FIRSTSEND"
					});
					this.getFirstVisit();
				}
			},
			firstVisit:{
				handler(curVal,oldVal){
					if (curVal.timeStamp != oldVal.timeStamp){
						this.getFirstVisit();
					}
				},
				deep:true
			}
		}
	}
	</script>
	<style type="text/css">
	.symptom{
		height: 700px;
		border: 1px solid #cccccc;
		background-color: #ffffff;
		text-align: left;
		padding-left: 10px;
		line-height: 30px;
	}
	.symptomContent{
		overflow: auto;
		height: 624px;
	}
	.symptom .el-checkbox__input.is-disabled+span.el-checkbox__label{
		color: #409EFF;
	}
	.mutexStyle1{
		color: #FFA500;
	}
	.mutexStyle2{
		color:#FF0000;
	}
	</style>